<?php

namespace App\Http\Controllers\Callback;

use App\Facades\EasyWechat;
use EasyWeChat\OpenPlatform\Server\Guard;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\App;
use \EasyWeChat\Kernel\Messages\Message;


/**
 * 微信开放平台回调入口控制器
 * @package App\Http\Controllers\Wechat
 */
class OpenController extends Controller
{
    /**
     * 处理微信的请求消息
     * @return string
     */
    public function serve($appid)
    {
        try {
            $openPlatform = EasyWechat::openPlatform($appid);
            //文本消息
            $openPlatform->server->push(function ($message) use($appid) {
                // 全网发布检测需要开启才能通过检测
                if (config('wechat.wx_open_release_test')){
                    if ($message['Content'] == 'TESTCOMPONENT_MSG_TYPE_TEXT'){
                        return "TESTCOMPONENT_MSG_TYPE_TEXT_callback";
                    }

                    if (strpos($message['Content'], 'QUERY_AUTH_CODE:') !== false){
                        $openPlatforms = EasyWechat::openPlatform($appid);
                        $query_data = explode(':', $message['Content']);
                        $openPlatforms->handleAuthorize($query_data[1]);
                        $authorizer = $openPlatforms->getAuthorizer($appid);
                        $officialAccounts = $openPlatforms->officialAccount($appid, $authorizer['authorization_info']['authorizer_refresh_token']);
                        $data = new \EasyWeChat\Kernel\Messages\Text($query_data[1].'_from_api');
                        $officialAccounts->customer_service->message($data)->to($message['FromUserName'])->send();
                    }
                }
                return '';
            }, Message::TEXT);
            //处理授权成功事件
            $openPlatform->server->push(function ($message) use($appid) {
                return (new \App\Services\WechatHandler\OpenAuthorizedHandler($appid))->handle($message);
            }, Guard::EVENT_AUTHORIZED);

            //处理授权更新事件
            $openPlatform->server->push(function ($message) use($appid) {
                return (new \App\Services\WechatHandler\OpenUpdateAuthorizedHandler($appid))->handle($message);
            }, Guard::EVENT_UPDATE_AUTHORIZED);

            //处理授权取消事件
            $openPlatform->server->push(function ($message) use($appid) {
                return (new \App\Services\WechatHandler\OpenUnauthorizedHandler($appid))->handle($message);
            }, Guard::EVENT_UNAUTHORIZED);

            //处理component_verify_ticket事件,微信每10分钟推送一次
            $openPlatform->server->push(function ($message) use($appid) {
                return (new \App\Services\WechatHandler\OpenVerifyTicketRefreshedHandler($appid))->handle($message);
            }, Guard::EVENT_COMPONENT_VERIFY_TICKET);

            $response = $openPlatform->server->serve();

            //将响应输出
            $response->send();
        } catch (\Exception $e) {
            Log::info($e->getMessage());
            //生产环境不报错
            if (App::environment('production')) {
                echo 'success';
            } else {
                throw new \Exception($e->getMessage());
            }
        }
    }
}
